package com.pshcp.service;

import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.framework.util.SystemUtil;
import com.github.pagehelper.PageHelper;
import com.pshcp.common.enums.CommonStatusEnum;
import com.pshcp.domain.dto.DoctorDiagnosisSettingsDTO;
import com.pshcp.domain.model.DoctorDiagnosisSettings;
import com.pshcp.mapper.DoctorDiagnosisSettingsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 专家问诊设置表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-02
 */
@Service
public class DoctorDiagnosisSettingsService {

    @Autowired
    private DoctorDiagnosisSettingsMapper doctorDiagnosisSettingsMapper;

    public DoctorDiagnosisSettings getDoctorDiagnosisSettingsByDoctorId(Long doctorId) {
        Example example = new Example(DoctorDiagnosisSettings.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("doctorId", doctorId);
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        example.setOrderByClause(" created_time desc");
        List<DoctorDiagnosisSettings> list = doctorDiagnosisSettingsMapper.selectByExample(example);
        return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    public PageList<DoctorDiagnosisSettings> list(DoctorDiagnosisSettingsDTO doctorDiagnosisSettingsDTO) {
        if (doctorDiagnosisSettingsDTO.getPageNum() != null && doctorDiagnosisSettingsDTO.getPageSize() != null) {
            PageHelper.startPage(doctorDiagnosisSettingsDTO.getPageNum(), doctorDiagnosisSettingsDTO.getPageSize());
        }
        Example example = new Example(DoctorDiagnosisSettings.class);
        Example.Criteria criteria = example.createCriteria();
        if (doctorDiagnosisSettingsDTO.getStatus() != null) {
            criteria.andEqualTo("status", doctorDiagnosisSettingsDTO.getStatus());
        }
        example.setOrderByClause(" created_time desc");
        List<DoctorDiagnosisSettings> list = doctorDiagnosisSettingsMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<DoctorDiagnosisSettings> select(DoctorDiagnosisSettings doctorDiagnosisSettings) {
        return doctorDiagnosisSettingsMapper.select(doctorDiagnosisSettings);
    }

    public int insert(DoctorDiagnosisSettings doctorDiagnosisSettings) {
        doctorDiagnosisSettings.setId(SystemUtil.getId());
        doctorDiagnosisSettings.setStatus(CommonStatusEnum.INIT.getCode());
        doctorDiagnosisSettings.setCreatedTime(new Date());
        return doctorDiagnosisSettingsMapper.insert(doctorDiagnosisSettings);
    }

    public int update(DoctorDiagnosisSettings doctorDiagnosisSettings) {
        doctorDiagnosisSettings.setUpdateTime(new Date());
        return doctorDiagnosisSettingsMapper.updateByPrimaryKeySelective(doctorDiagnosisSettings);
    }

    public DoctorDiagnosisSettings findById(Long id) {
        return doctorDiagnosisSettingsMapper.selectByPrimaryKey(id);
    }

    public List<DoctorDiagnosisSettings> findByIds(List<Long> ids) {
        Example example = new Example(DoctorDiagnosisSettings.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return doctorDiagnosisSettingsMapper.selectByExample(example);
    }

    public int delById(Long id) {
        Example example = new Example(DoctorDiagnosisSettings.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        DoctorDiagnosisSettings doctorDiagnosisSettings = new DoctorDiagnosisSettings();
        doctorDiagnosisSettings.setStatus(CommonStatusEnum.DEL.getCode());
        return doctorDiagnosisSettingsMapper.updateByExampleSelective(doctorDiagnosisSettings, example);
    }

}
